

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/icon.png">
  <link rel="icon" href="/img/icon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="d-lue">
  <meta name="keywords" content="">
  
    <meta name="description" content="HOOKS在函数组件中使用state以及其他React特性，钩子 123(1). State Hook: React.useState()(2). Effect Hook: React.useEffect()(3). Ref Hook: React.useRef()  123456789State Hook：(1). State Hook让函数组件也可以有state状态, 并进行状态数据的读写操作">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>React笔记（二） - d-lue&#39; blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"dl_coisini.gitee.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":"9c32ca7145bad202032b544e8bf27d19","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  

  
    <!-- Baidu Analytics -->
    <script async>
      if (!Fluid.ctx.dnt) {
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?9c32ca7145bad202032b544e8bf27d19";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
      }
    </script>
  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>d-lue&#39;s blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/bg/wenzhang.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="React笔记（二）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-09-07 14:18" pubdate>
          2023年9月7日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.7k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          64 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">React笔记（二）</h1>
            
            
              <div class="markdown-body">
                
                <h3 id="HOOKS"><a href="#HOOKS" class="headerlink" title="HOOKS"></a>HOOKS</h3><p>在函数组件中使用state以及其他React特性，钩子</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js">(<span class="hljs-number">1</span>). <span class="hljs-title class_">State</span> <span class="hljs-title class_">Hook</span>: <span class="hljs-title class_">React</span>.<span class="hljs-title function_">useState</span>()<br>(<span class="hljs-number">2</span>). <span class="hljs-title class_">Effect</span> <span class="hljs-title class_">Hook</span>: <span class="hljs-title class_">React</span>.<span class="hljs-title function_">useEffect</span>()<br>(<span class="hljs-number">3</span>). <span class="hljs-title class_">Ref</span> <span class="hljs-title class_">Hook</span>: <span class="hljs-title class_">React</span>.<span class="hljs-title function_">useRef</span>()<br></code></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title class_">State</span> <span class="hljs-title class_">Hook</span>：<br>(<span class="hljs-number">1</span>). <span class="hljs-title class_">State</span> <span class="hljs-title class_">Hook</span>让函数组件也可以有state状态, 并进行状态数据的读写操作<br>(<span class="hljs-number">2</span>). 语法: <span class="hljs-keyword">const</span> [xxx, setXxx] = <span class="hljs-title class_">React</span>.<span class="hljs-title function_">useState</span>(initValue)  <br>(<span class="hljs-number">3</span>). <span class="hljs-title function_">useState</span>()说明:<br>        参数: 第一次初始化指定的值在内部作缓存<br>        返回值: 包含<span class="hljs-number">2</span>个元素的数组, 第<span class="hljs-number">1</span>个为内部当前状态值, 第<span class="hljs-number">2</span>个为更新状态值的函数<br>(<span class="hljs-number">4</span>). <span class="hljs-title function_">setXxx</span>()<span class="hljs-number">2</span>种写法:<br>        <span class="hljs-title function_">setXxx</span>(newValue): 参数为非函数值, 直接指定新的状态值, 内部用其覆盖原来的状态值<br>        <span class="hljs-title function_">setXxx</span>(<span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> newValue): 参数为函数, 接收原本的状态值, 返回新的状态值, 内部用其覆盖原来的状态值（同时写三个setXxx，会执行三次，结果是三次的累加  第一种写法不会）<br></code></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title class_">Effect</span> <span class="hljs-title class_">Hook</span>：<br>(<span class="hljs-number">1</span>). <span class="hljs-title class_">Effect</span> <span class="hljs-title class_">Hook</span> 可以让你在函数组件中执行副作用操作(用于模拟类组件中的生命周期钩子)<br>(<span class="hljs-number">2</span>). <span class="hljs-title class_">React</span>中的副作用操作:<br>        发ajax请求数据获取<br>        设置订阅 / 启动定时器<br>        手动更改真实<span class="hljs-variable constant_">DOM</span><br>(<span class="hljs-number">3</span>). 语法和说明: <br>        <span class="hljs-title function_">useEffect</span>(<span class="hljs-function">() =&gt;</span> &#123; <br>          <span class="hljs-comment">// 在此可以执行任何带副作用操作</span><br>          <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> &#123; <span class="hljs-comment">// 在组件卸载前执行 beforeUpdata willUnmount</span><br>            <span class="hljs-comment">// 在此做一些收尾工作, 比如清除定时器/取消订阅等</span><br>          &#125;<br>        &#125;, [stateValue]) <span class="hljs-comment">// 如果指定的是[], 回调函数只会在第一次render()后执行</span><br>    <br>(<span class="hljs-number">4</span>). 可以把 useEffect <span class="hljs-title class_">Hook</span> 看做如下三个函数的组合<br>        <span class="hljs-title function_">componentDidMount</span>()<br>        <span class="hljs-title function_">componentDidUpdate</span>()<br>    	<span class="hljs-title function_">componentWillUnmount</span>() <br></code></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title class_">Ref</span> <span class="hljs-title class_">Hook</span>：<br>(<span class="hljs-number">1</span>). <span class="hljs-title class_">Ref</span> <span class="hljs-title class_">Hook</span>可以在函数组件中存储/查找组件内的标签或任意其它数据<br>(<span class="hljs-number">2</span>). 语法: <span class="hljs-keyword">const</span> refContainer = <span class="hljs-title function_">useRef</span>()<br>(<span class="hljs-number">3</span>). 作用:保存标签对象,功能与<span class="hljs-title class_">React</span>.<span class="hljs-title function_">createRef</span>()一样<br><br>还可以给普通值记忆操作<br></code></pre></td></tr></table></figure>

<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs jsx"><span class="hljs-attr">useContext</span>:<br>父组件-孙组件通信 如下文context<br></code></pre></td></tr></table></figure>

<figure class="highlight mercury"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs mercury">React.<span class="hljs-meta">memo</span><br>函数组件修改同一个值，不会重新渲染 使用React.<span class="hljs-meta">memo</span>优化<br></code></pre></td></tr></table></figure>

<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs jsx">useCallback：<br>返回一个可记忆的函数<br>useMomo：<br>返回一个可记忆的值，useCallback是useMomo的一种特殊形式<br><span class="hljs-keyword">const</span> foo = <span class="hljs-title function_">sueCallback</span>(<span class="hljs-function">() =&gt;</span> &#123;&#125;,[])<br><span class="hljs-keyword">const</span> foo = <span class="hljs-title function_">useMomo</span>(<span class="hljs-function">() =&gt;</span> [],[])<br>当点击按钮想修改一个对象、数组时，即使没有修改组件也会再次渲染，因为对象存储的内存地址，相当于修改了地址，所以会渲染。<br></code></pre></td></tr></table></figure>





<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs jsx">自定义<span class="hljs-title class_">Hook</span>：<br><span class="hljs-comment">// 获取坐标的Hook：</span><br><span class="hljs-comment">// 使用use开头，可使用useState、useEffect等hook</span><br><span class="hljs-keyword">let</span> <span class="hljs-title function_">useMouseXY</span> = (<span class="hljs-params"></span>) =&gt; &#123; <br><span class="hljs-comment">/* 函数体 */</span><br><span class="hljs-keyword">const</span> [x,setX] = <span class="hljs-title function_">useState</span>(<span class="hljs-number">0</span>)<br><span class="hljs-keyword">const</span> [y,setY] = <span class="hljs-title function_">useState</span>(<span class="hljs-number">0</span>)<br><span class="hljs-title function_">useEffect</span>(<span class="hljs-function">() =&gt;</span> &#123; <br>    <span class="hljs-comment">/* 函数体 */</span><br>    <span class="hljs-keyword">function</span> <span class="hljs-title function_">move</span>(<span class="hljs-params">ev</span>)&#123;<br>        <span class="hljs-title function_">setX</span>(ev.<span class="hljs-property">pageX</span>)<br>        <span class="hljs-title function_">setY</span>(ev.<span class="hljs-property">pageY</span>)<br>    &#125;<br>    <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">&quot;mousemove&quot;</span>,move)<br>    <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> &#123; <br>        <span class="hljs-comment">/* 删除事件 */</span><br>        <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">&quot;mousemove&quot;</span>,move)<br>    &#125;<br>&#125;,[])<br><span class="hljs-keyword">return</span> &#123;x,y&#125;<br>&#125;<br><br><span class="hljs-comment">//使用</span><br><span class="hljs-keyword">const</span> &#123;x,y&#125; = <span class="hljs-title function_">useMouseXY</span>()<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(x,y);<br><br></code></pre></td></tr></table></figure>



<hr>
<h3 id="Component的2个问题"><a href="#Component的2个问题" class="headerlink" title="Component的2个问题"></a>Component的2个问题</h3><blockquote>
<ol>
<li><p>只要执行setState(),即使不改变状态数据, 组件也会重新render() &#x3D;&#x3D;&gt; 效率低</p>
</li>
<li><p>只当前组件重新render(), 就会自动重新render子组件，纵使子组件没有用到父组件的任何数据 &#x3D;&#x3D;&gt; 效率低</p>
</li>
</ol>
</blockquote>
<p>效率高的做法</p>
<blockquote>
<p> 只有当组件的state或props数据发生改变时才重新render()</p>
</blockquote>
<p>原因</p>
<blockquote>
<p> Component中的shouldComponentUpdate()总是返回true</p>
</blockquote>
<p>解决</p>
<pre><code class="hljs">办法1: 
    重写shouldComponentUpdate()方法
    比较新旧state或props数据, 如果有变化才返回true, 如果没有返回false
办法2:  
    使用PureComponent
    PureComponent重写了shouldComponentUpdate(), 只有state或props数据有变化才返回true
    注意: 
        只是进行state和props数据的浅比较, 如果只是数据对象内部数据变了, 返回false  
        不要直接修改state数据, 而是要产生新数据
项目中一般使用PureComponent来优化
</code></pre>
<hr>
<h3 id="children-props、render-props"><a href="#children-props、render-props" class="headerlink" title="children props、render props"></a>children props、render props</h3><p>children props</p>
<pre><code class="hljs">&lt;A&gt;
  &lt;B&gt;xxxx&lt;/B&gt;
&lt;/A&gt;
&#123;this.props.children&#125;
问题: 如果B组件需要A组件内的数据, ==&gt; 做不到 
</code></pre>
<p>render props</p>
<pre><code class="hljs">&lt;A render=&#123;(data) =&gt; &lt;C data=&#123;data&#125;&gt;&lt;/C&gt;&#125;&gt;&lt;/A&gt;
A组件: &#123;this.props.render(内部state数据)&#125;
C组件: 读取A组件传入的数据显示 &#123;this.props.data&#125; 
</code></pre>
<h3 id="错误边界"><a href="#错误边界" class="headerlink" title="错误边界"></a>错误边界</h3><p>错误边界(Error boundary)：用来捕获后代组件错误，渲染出备用页面</p>
<p>特点：</p>
<p>只能捕获后代组件生命周期产生的错误，不能捕获自己组件产生的错误和其他组件在合成事件、定时器中产生的错误</p>
<p>使用方式：</p>
<p>getDerivedStateFromError配合componentDidCatch</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 生命周期函数，一旦后台组件报错，就会触发</span><br><span class="hljs-keyword">static</span> <span class="hljs-title function_">getDerivedStateFromError</span>(<span class="hljs-params">error</span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(error);<br>    <span class="hljs-comment">// 在render之前触发</span><br>    <span class="hljs-comment">// 返回新的state</span><br>    <span class="hljs-keyword">return</span> &#123;<br>        <span class="hljs-attr">hasError</span>: <span class="hljs-literal">true</span>,<br>    &#125;;<br>&#125;<br><br><span class="hljs-title function_">componentDidCatch</span>(<span class="hljs-params">error, info</span>) &#123;<br>    <span class="hljs-comment">// 统计页面的错误。发送请求发送到后台去</span><br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(error, info);<br>&#125;<br></code></pre></td></tr></table></figure>



<hr>
<h3 id="通信方式"><a href="#通信方式" class="headerlink" title="通信方式"></a>通信方式</h3><p>组件间的关系：</p>
<ul>
<li>父子组件</li>
<li>兄弟组件（非嵌套组件）</li>
<li>祖孙组件（跨级组件）</li>
</ul>
<p>几种通信方式：</p>
<pre><code class="hljs">    1.props：
        (1).children props
        (2).render props
    2.消息订阅-发布：
        pubs-sub、event等等
    3.集中式管理：
        redux、dva等等
    4.conText:
        生产者-消费者模式
</code></pre>
<p>比较好的搭配方式：</p>
<pre><code class="hljs">    父子组件：props
    兄弟组件：消息订阅-发布、集中式管理
    祖孙组件(跨级组件)：消息订阅-发布、集中式管理、conText(开发用的少，封装插件用的多)
</code></pre>
<p>消息订阅与发布：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//接收数据的订阅（subscribe），发送数据的发布（publish）</span><br><span class="hljs-comment">//下载: npm install pubsub-js --save</span><br><span class="hljs-comment">//使用: </span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">PubSub</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;pubsub-js&#x27;</span> <span class="hljs-comment">//引入</span><br><span class="hljs-title class_">PubSub</span>.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">&#x27;delete&#x27;</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params">fnName,data</span>)&#123; &#125;); <span class="hljs-comment">//订阅</span><br><span class="hljs-title class_">PubSub</span>.<span class="hljs-title function_">publish</span>(<span class="hljs-string">&#x27;delete&#x27;</span>, data) <span class="hljs-comment">//发布消息</span><br><br></code></pre></td></tr></table></figure>



<p>context：一种组件间通信方式, 常用于【祖组件】与【后代组件】间通信</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-number">1</span>) 创建<span class="hljs-title class_">Context</span>容器对象：<br>	<span class="hljs-keyword">const</span> <span class="hljs-title class_">XxxContext</span> = <span class="hljs-title class_">React</span>.<span class="hljs-title function_">createContext</span>([默认值])  <br>	<br><span class="hljs-number">2</span>) 渲染子组时，外面包裹xxxContext.<span class="hljs-property">Provider</span>, 通过value属性给后代组件传递数据：<br>	&lt;xxxContext.<span class="hljs-property">Provider</span> value=&#123;数据&#125;&gt;<br>		子组件<br>    &lt;/xxxContext.<span class="hljs-property">Provider</span>&gt;<br>    <br><span class="hljs-number">3</span>) 后代组件读取数据：<br><br>	<span class="hljs-comment">//第一种方式:仅适用于类组件 </span><br>	  <span class="hljs-keyword">static</span> contextType = xxxContext  <span class="hljs-comment">// 声明接收context</span><br>	  <span class="hljs-variable language_">this</span>.<span class="hljs-property">context</span> <span class="hljs-comment">// 读取context中的value数据</span><br>	  <br>	<span class="hljs-comment">//第二种方式: 函数组件与类组件都可以</span><br>	  &lt;xxxContext.<span class="hljs-property">Consumer</span>&gt;<br>	    &#123;<br>	      <span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> ( <span class="hljs-comment">// value就是context中的value数据</span><br>	        要显示的内容<br>	      )<br>	    &#125;<br>	  &lt;/xxxContext.<span class="hljs-property">Consumer</span>&gt;<br>	 <span class="hljs-comment">//第三种方式 </span><br>	 <span class="hljs-keyword">let</span> value = <span class="hljs-title function_">useContext</span>(xxxContext)<br>在应用开发中一般不用context, 一般都用它的封装react插件<br></code></pre></td></tr></table></figure>



<hr>
<h3 id="react-router6"><a href="#react-router6" class="headerlink" title="react-router6"></a>react-router6</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs jsx">&lt;<span class="hljs-title class_">Routes</span>&gt;<br>    <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;home&quot;</span> <span class="hljs-attr">element</span>=<span class="hljs-string">&#123;</span>&lt;<span class="hljs-attr">Home</span>/&gt;</span>&#125;&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span><br>    <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;about&quot;</span> <span class="hljs-attr">element</span>=<span class="hljs-string">&#123;</span>&lt;<span class="hljs-attr">About</span>/&gt;</span>&#125;&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span><br>&lt;/<span class="hljs-title class_">Routes</span>&gt;<br></code></pre></td></tr></table></figure>

<p>重定向：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs jsx">&lt;<span class="hljs-title class_">Route</span> path=<span class="hljs-string">&quot;/&quot;</span> element=&#123;<span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Navigate</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/home&quot;</span> /&gt;</span></span>&#125;&gt;&lt;/<span class="hljs-title class_">Route</span>&gt;<br><br><span class="hljs-comment">//sum等于2 跳转about</span><br>&#123;<br>    sum === <span class="hljs-number">2</span> ? <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Navigate</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/about&quot;</span>/&gt;</span></span>: <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>he: $&#123;sum&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br>&#125;<br></code></pre></td></tr></table></figure>

<p>路由表：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs jsx"><span class="hljs-comment">// 路由表</span><br><span class="hljs-keyword">const</span> element = <span class="hljs-title function_">useRoutes</span>([<br>    &#123;<br>        <span class="hljs-attr">path</span>:<span class="hljs-string">&quot;/&quot;</span>,<br>        <span class="hljs-attr">element</span>:<span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Navigate</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/home&quot;</span> /&gt;</span></span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-attr">path</span>:<span class="hljs-string">&quot;/home&quot;</span>,<br>        <span class="hljs-attr">element</span>:<span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Home</span>/&gt;</span></span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-attr">path</span>:<span class="hljs-string">&quot;/about&quot;</span>,<br>        <span class="hljs-attr">element</span>:<span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">About</span>/&gt;</span></span><br>    &#125;,<br>])<br><br>使用：<br>&lt;div className=<span class="hljs-string">&quot;panel-body&quot;</span>&gt;<br>    &#123;element&#125;<br>&lt;/div&gt;<br></code></pre></td></tr></table></figure>



<p>路由参数：</p>
<figure class="highlight pf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs pf"><span class="hljs-number">1</span>.params<br><br><span class="hljs-number">2</span>.search<br><br><span class="hljs-number">3</span>.<span class="hljs-keyword">state</span><br></code></pre></td></tr></table></figure>





<hr>
<p>react18</p>
<figure class="highlight gherkin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs gherkin">1.<span class="hljs-variable">&lt;&gt;</span> <span class="hljs-variable">&lt;/&gt;</span><br>2.app.jsx:无需引入 import React from <span class="hljs-string">&quot;react&quot;</span><br>3.严格模式：有提示（过时的语法.....）<br><span class="hljs-variable">&lt;React.StrictMode&gt;</span><br>  <span class="hljs-variable">&lt;App /&gt;</span><br><span class="hljs-variable">&lt;/React.StrictMode&gt;</span><br><br></code></pre></td></tr></table></figure>



<h3 id="react-transition-group"><a href="#react-transition-group" class="headerlink" title="react-transition-group"></a>react-transition-group</h3><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-class">.Animate</span>&#123;<br>  <span class="hljs-attribute">width</span>: <span class="hljs-number">500px</span>;<br>  <span class="hljs-attribute">margin</span>: <span class="hljs-number">50px</span> auto;<br>&#125;<br><br><span class="hljs-selector-class">.box</span>&#123;<br>  <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;<br>  <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;<br>  <span class="hljs-attribute">background-color</span>: red;<br>&#125;<br><span class="hljs-comment">/* 入场 */</span><br><span class="hljs-selector-class">.fade-enter</span>&#123;<br>  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0</span>;<br>&#125;<br><span class="hljs-selector-class">.fade-enter-active</span>&#123;<br>  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">1</span>;<br>  <span class="hljs-attribute">transition</span>: <span class="hljs-number">1s</span>;<br>&#125;<br><span class="hljs-selector-class">.fade-enter-done</span>&#123;<br>  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">1</span>;<br>&#125;<br><span class="hljs-comment">/* 出场 */</span><br><span class="hljs-selector-class">.fade-exit</span>&#123;<br>  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">1</span>;<br>&#125;<br><span class="hljs-selector-class">.fade-exit-active</span>&#123;<br>  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0</span>;<br>  <span class="hljs-attribute">transition</span>: <span class="hljs-number">1s</span>;<br>&#125;<br><span class="hljs-selector-class">.fade-exit-done</span>&#123;<br>  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0</span>;<br>&#125;<br><span class="hljs-comment">/* 初始 */</span><br><span class="hljs-selector-class">.fade-appear</span>&#123;<br>  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">1</span>appear<br>&#125;<br><span class="hljs-selector-class">.fade-appear-active</span>&#123;<br>  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0</span>;<br>  <span class="hljs-attribute">transition</span>: <span class="hljs-number">1s</span>;<br>&#125;<br><span class="hljs-selector-class">.fade-appear-done</span>&#123;<br>  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs jsx"><span class="hljs-title class_">App</span>.<span class="hljs-property">jsx</span>:<br><span class="hljs-keyword">import</span> &#123;<span class="hljs-title class_">CSSTransition</span>&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;react-transition-group&quot;</span><br><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;./App.css&quot;</span><br><span class="hljs-keyword">import</span> &#123; useRef, useState &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;react&quot;</span>;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">App</span>(<span class="hljs-params"></span>) &#123;<br>  <span class="hljs-keyword">const</span> [pop,setPop] = <span class="hljs-title function_">useState</span>(<span class="hljs-literal">true</span>)<br>  <span class="hljs-keyword">const</span> nodeRef = <span class="hljs-title function_">useRef</span>(<span class="hljs-literal">null</span>)<br>  <span class="hljs-keyword">const</span> <span class="hljs-title function_">handleStart</span> = (<span class="hljs-params"></span>) =&gt; &#123; <br>    <span class="hljs-comment">/* 函数体 */</span><br>    <span class="hljs-title function_">setPop</span>(!pop)<br>  &#125;<br>  <span class="hljs-keyword">const</span> <span class="hljs-title function_">handleEntered</span> = (<span class="hljs-params"></span>) =&gt; &#123; <br>    <span class="hljs-comment">/* 函数体 */</span><br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;123&quot;</span>);<br>  &#125;<br>  <span class="hljs-keyword">return</span> (<br>    <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;Animate&quot;</span>&gt;</span></span><br><span class="language-xml">      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">&#123;handleStart&#125;</span>&gt;</span>dainji<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span><br><span class="language-xml">      <span class="hljs-tag">&lt;<span class="hljs-name">CSSTransition</span> <span class="hljs-attr">in</span>=<span class="hljs-string">&#123;pop&#125;</span> <span class="hljs-attr">timeout</span>=<span class="hljs-string">&#123;1000&#125;</span> <span class="hljs-attr">classNames</span>=<span class="hljs-string">&quot;fade&quot;</span> <span class="hljs-attr">nodeRef</span>=<span class="hljs-string">&#123;nodeRef&#125;</span> <span class="hljs-attr">unmountOnExit</span> <span class="hljs-attr">onEntered</span>=<span class="hljs-string">&#123;handleEntered&#125;</span> <span class="hljs-attr">appear</span>&gt;</span></span><br><span class="language-xml">        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;box&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&#123;nodeRef&#125;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span><br><span class="language-xml">      <span class="hljs-tag">&lt;/<span class="hljs-name">CSSTransition</span>&gt;</span></span><br><span class="language-xml">    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span><br>  );<br>&#125;<br><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-title class_">App</span>;<br><br><br><br><span class="hljs-comment">// unmountOnExit:隐藏后销毁组件 onEntered：入场结束后的事件  appear：初始动画</span><br></code></pre></td></tr></table></figure>





<h3 id="createPortal-传送门"><a href="#createPortal-传送门" class="headerlink" title="createPortal 传送门"></a>createPortal 传送门</h3><p>ReactDOM.createPortal() 将子节点渲染到已存在DOM节点中的方式，实现传送门功能</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs jsx"><span class="hljs-comment">// 适合全局的 例如消息通知....</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Message</span>(<span class="hljs-params"></span>)&#123;<br>  <span class="hljs-keyword">return</span> <span class="hljs-title class_">ReactDOM</span>.<span class="hljs-title function_">createPortal</span>(<br>    <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span></span><br><span class="language-xml">      123</span><br><span class="language-xml">    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>,<br>    <span class="hljs-variable language_">document</span>.<span class="hljs-property">body</span> <span class="hljs-comment">// 传送位置</span><br>  )<br>&#125;<br></code></pre></td></tr></table></figure>



<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs reasonml">封装消息组件：<br>可以使用传送门，也可使用<span class="hljs-module-access"><span class="hljs-module"><span class="hljs-identifier">ReactDOM</span>.</span></span>createRoot+render<br>const message = <span class="hljs-module-access"><span class="hljs-module"><span class="hljs-identifier">ReactDOM</span>.</span></span>create<span class="hljs-constructor">Root(<span class="hljs-params">document</span>.<span class="hljs-params">querySelector</span>(<span class="hljs-string">&quot;#message&quot;</span>)</span>)<br>message.render(&lt;Message text=&#123;text&#125; icon=<span class="hljs-string">&quot;√&quot;</span>/&gt;)<br></code></pre></td></tr></table></figure>



<h3 id="脚手架下样式的处理"><a href="#脚手架下样式的处理" class="headerlink" title="脚手架下样式的处理"></a>脚手架下样式的处理</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs jsx">不冲突：<br><span class="hljs-number">1.</span>增加类名<br>.<span class="hljs-property">app</span> .<span class="hljs-property">box</span>&#123;<br><br>&#125;<br>.<span class="hljs-property">count</span> .<span class="hljs-property">box</span>&#123;<br>    <br>&#125;<br><br><span class="hljs-number">2.</span>scss：<br>.<span class="hljs-property">app</span>&#123;<br>    .<span class="hljs-property">box</span>&#123;<br>        <span class="hljs-attr">width</span>: 100px;<br>        <span class="hljs-attr">height</span>: 100px;<br>        background-<span class="hljs-attr">color</span>: blue;<br>    &#125;<br>&#125;<br><br><span class="hljs-number">3.</span>模块化：<br>命名：<br>app.<span class="hljs-property">module</span>.<span class="hljs-property">css</span><br>count.<span class="hljs-property">module</span>.<span class="hljs-property">css</span><br>引入：<br><span class="hljs-keyword">import</span> style <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;./count.module.css&quot;</span><br>赋值：<br>&lt;div className=&#123;style.<span class="hljs-property">box</span>&#125;&gt;&lt;/div&gt;<br><br><span class="hljs-number">4.</span><span class="hljs-variable constant_">CSS</span>-<span class="hljs-keyword">in</span>-<span class="hljs-attr">JS</span>:<br>安装：npm i styled-components<br>引入：<span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;styled-components&quot;</span>;<br>定义：<br><span class="hljs-keyword">const</span> <span class="hljs-title class_">Title</span> =styled.<span class="hljs-property">h1</span><span class="hljs-string">`</span><br><span class="hljs-string">  width: 100px;</span><br><span class="hljs-string">  height: 100px;</span><br><span class="hljs-string">  background-color: blue;</span><br><span class="hljs-string">`</span>;<br>使用：&lt;<span class="hljs-title class_">Title</span>&gt;<span class="hljs-number">123</span>&lt;/<span class="hljs-title class_">Title</span>&gt;<br><br><span class="hljs-number">5.</span>对象形式操作样式：<br>下载：npm i classnames<br>引入自己的样式：<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;./app.css&quot;</span><br>引入classnames：<span class="hljs-keyword">import</span> classnames <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;classnames&quot;</span><br>定义对象：<br><span class="hljs-keyword">const</span> myClass = <span class="hljs-title function_">classnames</span>(&#123;<br>  <span class="hljs-attr">box</span>:<span class="hljs-literal">true</span><br>&#125;)<br>使用：&lt;div className=&#123;myClass&#125;&gt;<span class="hljs-number">123</span>&lt;/div&gt;<br></code></pre></td></tr></table></figure>


                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/React/" class="category-chain-item">React</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/React/">#React</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>React笔记（二）</div>
      <div>https://dl_coisini.gitee.io/2023/09/07/reactNote2/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>d-lue</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年9月7日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/09/15/reactNote3/" title="React笔记（三）">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">React笔记（三）</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/08/29/reactNote1/" title="React笔记（一）">
                        <span class="hidden-mobile">React笔记（一）</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body>
</html>
